package com.sample.springbatch.controller;


import com.sample.springbatch.common.MyException;
import com.sample.springbatch.job.listener.MapExecutionTracker;
import com.sample.springbatch.job.service.BatchJobService;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author zwg
 */

@RestController
@RequestMapping("batch")
public class BatchController {

    @Autowired
    BatchJobService batchJobService;

    @Autowired
    MapExecutionTracker mapExecutionTracker;




    @RequestMapping(value = "/startJob")
    public String startJob(@RequestParam(defaultValue = "") String jobName){
        String params = "sys_time:"+new Date().toString();
        JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter();
        JobParameters jobParameters = jobParametersConverter.getJobParameters(PropertiesConverter.stringToProperties(params));

        if(StringUtils.isEmpty(jobName)){
            jobName="demoStartJob";
            return "jobName is null";
        }
        JobExecution jobExecution = null;
        try {
            jobExecution = batchJobService.launch(jobName,jobParameters,false);
        } catch (NoSuchJobException e) {
            e.printStackTrace();
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

        if(jobExecution!=null){
            return jobName + " run ok";
        }else{
            return jobName + " run fail";
        }

    }


    @RequestMapping(value = "/getJobMemLog")
    public String getJobMemLog(@RequestParam(defaultValue = "0") Long id){

        JobExecution job = mapExecutionTracker.getJobExecutionById(id);
        if(job==null){
            return "id "+id+" jobExecution is null";
        }
        return job.toString();
    }

    @RequestMapping(value = "/getStepMemLog")
    public String getStepMemLog(@RequestParam(defaultValue = "0") Long id){
        StepExecution step = mapExecutionTracker.getStepExecutionById(id);
        if(step==null){
            return "id "+id+" stepExecution is null";
        }
        return step.toString();
    }

    @RequestMapping(value = "/showLog")
    public String showLog(@RequestParam(defaultValue = "0") Long id){
        mapExecutionTracker.showJobExecutionMap(id);
        mapExecutionTracker.showStepExecutionMap(id);
        return "ok";
    }
}
